<?php
/**
 * Note: *
 */
namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Promo\BaiduService;
use App\Services\Promo\Douyin2Service;
use App\Services\Promo\DouyinService;
use App\Services\Promo\KuaiShouService;
use App\Services\Promo\TencentService;
use Illuminate\Http\Request;

class OauthController extends Controller
{
    /**
     * @param Request $request
     * @param string $channel
     * @return mixed
     */
    public function index(Request $request, string $channel)
    {
        if (!method_exists($this, $channel)) abort(404);

        return $this->$channel($request);
    }

    /**
     * 快手磁力开放平台授权
     * @param Request $request
     * @return string
     */
    public function kuaishou(Request $request): string
    {
        // 获取access_token, 并维持状态
        $service = new KuaiShouService();
        try {
            $accessToken = $service->getAccessToken($request->auth_code);
        } catch (\Exception $e) {
            report($e);
            return $e->getMessage();
        }
        info(__METHOD__, [$accessToken]);
        return '操作成功';
    }

    /**
     * 抖音巨量开放平台授权
     * @param Request $request
     * @return string
     */
    public function douyin(Request $request): string
    {
        // 获取access_token, 并维持状态
        $service = new DouyinService();
        try {
            $accessToken = $service->getAccessToken($request->auth_code);
        } catch (\Exception $e) {
            report($e);
            return $e->getMessage();
        }
        info(__METHOD__, [$accessToken]);
        return '操作成功';
    }

    /**
     * 抖音巨量开放平台授权
     * @param Request $request
     * @return string
     */
    public function douyin2(Request $request): string
    {
        // 获取access_token, 并维持状态
        $service = new Douyin2Service();
        try {
            $accessToken = $service->getAccessToken($request->auth_code);
        } catch (\Exception $e) {
            report($e);
            return $e->getMessage();
        }
        info(__METHOD__, [$accessToken]);
        return '操作成功';
    }

    /**
     * 百度营销平台授权
     * @param Request $request
     * @return string
     */
    public function baidu(Request $request)
    {
        // 获取access_token, 并维持状态
        $service = new BaiduService();
        try {
            $accessToken = $service->getAccessToken($request->authCode, $request->userId);
        } catch (\Exception $e) {
            report($e);
            return $e->getMessage();
        }
        info(__METHOD__, [$accessToken]);
        return '操作成功';
    }

    /**
     * 百度营销平台授权
     * @param Request $request
     * @return string
     */
    public function tencent(Request $request)
    {
        // 获取access_token, 并维持状态
        $service = new TencentService();
        try {
            $accessToken = $service->getAccessToken($request->authorization_code);
        } catch (\Exception $e) {
            report($e);
            return $e->getMessage();
        }
        info(__METHOD__, [$accessToken]);
        return '操作成功';
    }
}
